package TrafficSim;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class MapLayer extends JLabel{

	private ZoomAndPanListener z;
	private Image roadImage = null;
	private Graphics2D g2d;
	private BufferedImage mapImg=null;

	public MapLayer(ZoomAndPanListener z) {
		this.z = z;
		try {
			mapImg = ImageIO.read(new File("map78.png"));
		} catch (IOException e) {
		}
	}
	
	public void paint(Graphics g) {
		this.g2d = (Graphics2D) g;
		 if (z.somethingChanged()) {
		      // Create the road image if it doesn't exist
		      roadImage = createImage(super.getWidth(), super.getHeight());
		      // draw the roads to the image
		      g2d = (Graphics2D) roadImage.getGraphics();
		      // Use roadG like you would any other graphics
		      // object to draw the roads to an image
		      AffineTransform scale78 = new AffineTransform();
		      scale78.setTransform(z.getCoordTransform());
//		      scale78.scale(z.getCoordTransform().getScaleX()*0.78, z.getCoordTransform().getScaleY()*0.78);
//		      scale78.translate(z.getCoordTransform().getTranslateX()*0.78, z.getCoordTransform().getTranslateY()*0.78);
		      g2d.setTransform(scale78);
		      try{
		      g2d.drawImage(mapImg, 0, 0, null);
		      }
		      catch(OutOfMemoryError ex){
		    	  
		      }
		 } else {
		      // If the buffer image exists, you just need to draw it.
		      // Draw the road buffer image
		      g2d.drawImage(roadImage, 0, 0, null);
		      // Draw everything else ...
		      // g.draw...
		}
	}

}
